<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/2/25
 * Time: 11:16
 */
/*登录功能入口
http://127.0.0.1/cyxy/login.php*/

header('content-type:text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

//先屏蔽掉php打印出来的错误信息
error_reporting(E_ALL || ~E_NOTICE);
require_once 'response.php';
require_once  'db.php';

//接收PUT过来的账号和密码
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
//$username = isset($_GET['username']) ? $_GET['username'] : '';
//$password = isset($_GET['password']) ? $_GET['password'] : '';

//连接数据库
try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
//    $e->getMessage();
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

//预处理语句进行登录名和密码的验证
$sql = "SELECT user_type FROM user WHERE username=?&&password=?";
$mysqli_stmt = $connect->prepare($sql);
$mysqli_stmt->bind_param('ss',$username,$password);

$data = array(
    'IsUser' => false,
    'IsAdmin' => false
);
//执行sql语句
if($mysqli_stmt->execute()) {
    //结果绑定到$type上面
    $mysqli_stmt->bind_result($type);
    //如果有结果集，就进入 判断是 管理员 还是 普通用户
    if($mysqli_stmt->fetch()) {
        $data['IsUser'] = true;
            //返回IsAdmin，说明了用户类型
        if($type == 0) {
            $data['IsAdmin'] = true;
        } else {
            $data['IsAdmin'] = false;
        }
        return Response::show('200','登录成功',$data);
    } else {
        return Response::show('400','登录失败,用户名或密码错误',$data);
    }
} else {
    return Response::show('404','登录失败,服务器没响应',$data);
}

/*//释放结果集
$mysqli_stmt->free_result();
//关闭预处理语句
$mysqli_stmt->close();
//关闭连接
$connect->close();*/





